import Vue from 'vue'
import axios from 'axios'
import router from '@/router'
Vue.use(router);
Vue.prototype.$axios = axios;

// 拦截器配置
const myconfig = {
  code: -10,
  type: 'Bearer',
  yzUrl: 'http://game.54blues.com/wechatLogin/?url=', // 登陆验证跳转
  loginurl: 'http://game.54blues.com/red.html#/login', // 成功返回登陆
  header: {
    token: sessionStorage.getItem('token')
  }
};

axios.defaults.baseURL = 'http://car.adbpx.com';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 在请求或响应被 then 或 catch 处理前拦截它们
axios.interceptors.request.use((config) => {
  config.headers.ispc = 1;
  if (localStorage.getItem('token')) {
    config.headers.Authorization = myconfig.type + ' ' + localStorage.getItem('token');
  } else { // 登录过验证token
    loginRedirect();
  }
  return config;
}, err => {
  return Promise.reject(err);
});
// 请求后拦截器
axios.interceptors.response.use((result) => {
  if (result.data.status === myconfig.code) { // 未登录
    loginRedirect();
  }
  return result;
}, (err) => {
  return Promise.reject(err);
});
function loginRedirect () { // 跳转
  router.replace({name: 'login'});
}